/* SpanTree.h
 * Authors : Mayank Mishra, Prabhakar Ekde,   CSE IIT-Bombay
 */

#ifndef SPANTREE_H
#define SPANTREE_H

#define AM_ST_CONTROL_ASYNC   23
#define AM_ST_CONTROL_SYNC    27
#define AM_ST_DATA            13

#define ROUTE_TBL_MAX_ENTRIES           20

#define ROOT_MOTE_ID                    1
#define MAX_MOTE_ID                     20
#define INVALID_MOTE_ID                 0

#define MAX_PATH_VECTOR_LEN             10
#define INVALID_PATH_VECTOR_LEN         0

#define MAX_NUM_CHILDREN                10
#define MAX_NUM_POTENTIAL_PARENTS       5

#define STHB_BEACON_PERIOD              500 /* In milliseconds */
#define STHB_BEACON_TIMEOUT             5100
#define SEEK_RETRY_PERIOD               100
#define ROUTE_DISPLAY_PERIOD            2500 /* In milliseconds */
#define PARENT_SCAN_PERIOD              5000 /* In milliseconds */

#define MAX_SEEK_RETRIES                4

#define MAX_TOPOLOGY_ENTRIES            (MAX_MOTE_ID + 1)
#define MAX_TOPOLOGY_CONTROL_OPTIONS    2


#define PKT_DROP_PROBABILITY_CONTROL    20 /* range 0 (min) to 255(max) */
#define PKT_DROP_PROBABILITY_DATA       20


#define DEBUG_ROUTING   FALSE//TRUE
#define DEBUG_STATE     FALSE//TRUE
#define DEBUG_CONTROL   FALSE//TRUE
#define DEBUG_DATA      FALSE//TRUE
#define DEBUG_INFO      TRUE
#define DEBUG_ERROR     FALSE//TRUE

typedef enum SpanningTreeState {
    ORPHAN = 0,
    SCANNING_PARENT,
    SEEKING_PARENT,
    JOINED_PARENT,
    JOINED_PARENT_HAS_CHILDREN
} SpanningTreeState;

typedef struct SpanTreeRouteTableEntry {
  /* logic 2 */
  /*uint8_t nodeId;*/
    uint8_t nextHop;
    bool isValid;
} SpanTreeRouteTableEntry;

typedef struct SpanTreeRouteTable {
    SpanTreeRouteTableEntry routeEntry[ROUTE_TBL_MAX_ENTRIES];
} SpanTreeRouteTable;


typedef struct SpanTreeParent {
    uint8_t parentId;
    uint8_t pathVectorLength;
    uint8_t pathVector[MAX_PATH_VECTOR_LEN];
} SpanTreeParent;


typedef struct SpanTreePotentialParentList {
    SpanTreeParent potentialParentEntry[MAX_NUM_POTENTIAL_PARENTS];
    bool isValidPotentialParent[MAX_NUM_POTENTIAL_PARENTS];
} SpanTreePotentialParentList;

typedef struct SpanTreeChildrenList {
    uint8_t childEntry[MAX_NUM_CHILDREN];
    bool isValidChild[MAX_NUM_CHILDREN];
    /* Number of HB messages received from this child in last 
     * STHB_BEACON_TIMEOUT period
     */
    uint8_t numHBMsg[MAX_NUM_CHILDREN];
    uint8_t numChildren;
} SpanTreeChildrenList;

typedef struct SpanTreeControl {
    uint8_t state;
    SpanTreeParent parent;
    uint8_t numSTBeacFromParent;
    SpanTreeChildrenList children;
    SpanTreeRouteTable routeTable;
    SpanTreePotentialParentList potentialParents;
} SpanTreeControl;


#endif
